package com.metagurukul.informer.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.metagurukul.informer.utility.ConnectionFactory;
import com.metagurukul.informer.valueObjects.Users;

public class LoginUserDAO {

	/**
	 * 
	 * @param id
	 * @param password
	 * @return the requested user detail
	 * @throws SQLException
	 */
	public Users LoginUser(String id,String password) throws SQLException{
		if(id==null || password==null){
			throw new NullPointerException();
		}
		Users users=null;
		PreparedStatement ps=null;
		ResultSet rs = null;
		Connection con;
		String sql="select * from users where id=? and password=? ";
		
			con = ConnectionFactory.getInstance();
			ps=con.prepareStatement(sql);
			ps.setString(1, id);
			ps.setString(2, password);
			rs=ps.executeQuery();
			while(rs.next()){
				users=new Users(rs.getString(1),rs.getString(2),rs.getString(3),rs.getBoolean(4));
			}
			if(ps!=null){
				ps.close();
			}
			if(rs!=null){
				rs.close();
			}
			if(con!=null){
				con.close();
			}
		return users;
	}
	/**
	 * 
	 * @param id
	 * @return true if requested person is a member else false
	 * @throws SQLException
	 */
	public boolean isExistingUser(String id) throws SQLException {

		if(id==null){
			throw new NullPointerException();
		}
		boolean userExist = false;
	
		ResultSet rs = null;
		PreparedStatement ps=null;
		
			Connection con = ConnectionFactory.getInstance();
			String sql = "SELECT * FROM users WHERE id=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, id);
			rs=ps.executeQuery();
			
			if (rs.next()) {
				 userExist=true;
			}
			if(ps!=null){
				ps.close();
			}
			if(rs!=null){
				rs.close();
			}
			if(con!=null){
				con.close();
			}
		
		
		return userExist;
	}
	/**
	 * 
	 * @param id
	 * @return true if requested person is registered else false
	 * @throws SQLException
	 */
	public boolean isTempUser(String id) throws SQLException {
		
		
		if(id==null){
			throw new NullPointerException();
		}
		boolean userExist = false;
		
		ResultSet rs = null;
		PreparedStatement ps=null;
		
			Connection con = ConnectionFactory.getInstance();
			String sql = "SELECT * FROM tempUsers WHERE id=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, id);
			rs=ps.executeQuery();
			
			if (rs.next()) {
				 userExist=true;
			}
			if(ps!=null){
				ps.close();
			}
			if(rs!=null){
				rs.close();
			}
			if(con!=null){
				con.close();
			}
		
		
		return userExist;
	}
	/**
	 * register the user in tempUsers table
	 * @param id
	 * @param name
	 * @param password
	 * @return
	 * @throws SQLException
	 */
		private String registerUser(String id,String name,String password) throws SQLException {
		
		if(id==null || name==null || password==null){
			throw new NullPointerException();
		}
		PreparedStatement ps=null;
		String sql = "insert into users(id,name,password,isadmin) values (?,?,?,?)";
		
		Connection con;
		
			con = ConnectionFactory.getInstance();
			ps=con.prepareStatement(sql);
			ps.setString(1,id);
			ps.setString(2,name);
			ps.setString(3,password);
			ps.setBoolean(4, false);
			ps.executeUpdate();
			
			if(ps!=null){
				ps.close();
			}
		
			if(con!=null){
				con.close();
			}
			return password;

		
		
	}
		/**
		 * delete user from temp users and put in user table means now registered user
		 * become a member
		 * @param id
		 * @throws SQLException
		 */
		public void finalRegistration(String id)throws SQLException {
			ResultSet rs = null;
			PreparedStatement ps=null;
			String name=null;
			String password=null;
			if(id==null){
				throw new NullPointerException();
			}
			
			Connection con = ConnectionFactory.getInstance();
			String sql = "SELECT * FROM tempUsers WHERE id=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, id);
			rs=ps.executeQuery();
			
			if (rs.next()) {
				 name=rs.getString(2);
				 password=rs.getString(3);
			}

			if(ps!=null){
				ps.close();
			}
			if(rs!=null){
				rs.close();
			}
			if(con!=null){
				con.close();
			}
			
			if(name!=null && password!=null){
				try {
					registerUser(id, name, password);
					updateTempUser(id);
				} catch (SQLException e) {
					throw new SQLException();
				}
				
			}else{
				throw new SQLException();
			}
			
		}
		/**
		 * remove temp user detail
		 * @param id
		 * @throws SQLException
		 */
		private void updateTempUser(String id) throws SQLException{
			if(id==null){
				throw new NullPointerException();
			}
			PreparedStatement ps=null;
			
			Connection con = ConnectionFactory.getInstance();
			String sql = "delete from tempUsers WHERE id=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, id);
			ps.executeUpdate();
			
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
			
		}
		
}
